package com.farm.kaiping.init;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.farm.common.constant.CommonConstants;
import com.farm.kaiping.entity.SysOauthClientDetails;
import com.farm.kaiping.mapper.SysOauthClientDetailsMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class SysOauthClientInit {

    @Resource
    private SysOauthClientDetailsMapper sysOauthClientDetailsMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void init() {
        // 在这里执行你需要的操作
        LambdaQueryWrapper<SysOauthClientDetails> sysOauthClientDetailsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<SysOauthClientDetails> sysOauthClientDetailsList = sysOauthClientDetailsMapper.selectList(sysOauthClientDetailsLambdaQueryWrapper);
        Map<String, String> collect = sysOauthClientDetailsList.stream().collect(Collectors.toMap(SysOauthClientDetails::getClientId, SysOauthClientDetails::getClientSecret, (k1, k2) -> k1));
        collect.forEach((k, v) -> stringRedisTemplate.opsForValue().set(CommonConstants.OAUTH_CLIENT +k, v));
    }

}
